﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class DevTroubleNoteAdd : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(DevTroubleNoteAdd));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.DevPartfile.ToString();//"Devfile";

        #region 载入信息

        public void loadInfo()
        {
            DeviceInfo deviceinfo = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                deviceinfo = new DeviceInfo();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(263, ref sModelName, (clsBaseLib)deviceinfo))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(262, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(265, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = deviceinfo.getDevTroubleInfoById(m_Id);
                    //hdselect.Value = buy.getDeviceSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
    //                        a.ID, a.TroubleCode, a.DevID, a.TroubleCom, a.ProcessWay, a.TroubleReason, a.HappenTime,
    //a.RepairTime, a.NeedMoney, a.TroubleType, a.AddNoteMan, a.YingXiang,&nbsp;
    //a.ProcessDept, b.DeviceCode, b.DeviceName, b.Devicespec, c.DeptName
                        txtTroubleCode.Text = Convert.ToString(dt.Rows[0]["TroubleCode"]);
                        txtTroubleCode.Enabled = false;
                        txtDevID.Value = Convert.ToString(dt.Rows[0]["DevID"]);
                        lbDevCode.Text = Convert.ToString(dt.Rows[0]["DeviceCode"]);
                        Button_Dev.Visible = false;
                        lbDevName.Text = Convert.ToString(dt.Rows[0]["DeviceName"]);
                        lbUesDept.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        lbDevSpec.Text = Convert.ToString(dt.Rows[0]["Devicespec"]);

                        txtTroubleCom.Text = Convert.ToString(dt.Rows[0]["TroubleCom"]);
                        txtProcessWay.Text = Convert.ToString(dt.Rows[0]["ProcessWay"]);
                        txtTroubleReason.Text = Convert.ToString(dt.Rows[0]["TroubleReason"]);
                        
                        txtNeedMoney.Text = Convert.ToString(dt.Rows[0]["NeedMoney"]);
                        txtAddNoteMan.Text = Convert.ToString(dt.Rows[0]["AddNoteMan"]);
                        txtProcessDept.Text = Convert.ToString(dt.Rows[0]["ProcessDept"]);

                        if (dt.Rows[0]["HappenTime"] != null && dt.Rows[0]["HappenTime"].ToString().Trim() != "")
                        {
                            txtHappenTime.Text = Convert.ToDateTime(dt.Rows[0]["HappenTime"]).ToString("yyyy-MM-dd");
                        }
                        if (dt.Rows[0]["RepairTime"] != null && dt.Rows[0]["RepairTime"].ToString().Trim() != "")
                        {
                            txtRepairTime.Text = Convert.ToDateTime(dt.Rows[0]["RepairTime"]).ToString("yyyy-MM-dd");
                        }

                        dropTroubleType.SelectedValue = Convert.ToString(dt.Rows[0]["TroubleType"]);
                        dropYingXiang.SelectedValue = Convert.ToString(dt.Rows[0]["YingXiang"]);
                    }

                    #region //获取维修设备备件列表
                    dt = ds.Tables[1];
                    rptPart.DataSource = dt;
                    rptPart.DataBind();
                    #endregion
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        //[Ajax.AjaxMethod]
        //public DataTable getSelectDeviceInfo(string sIds)
        //{
        //    BuyDevice buy = null;
        //    DataTable dt = null;
        //    try
        //    {
        //        buy = new BuyDevice();
        //        dt = buy.getResultDeviceInfo(sIds);
        //    }
        //    catch
        //    {
        //        dt = null;
        //    }
        //    finally
        //    {
        //        if (buy != null)
        //        {
        //            buy.Dispose();
        //        }
        //    }
        //    return dt;
        //} 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int tid, int devid, string TroubleCode, string TroubleCom, string ProcessWay, string TroubleReason, string HappenTime, string RepairTime, string NeedMoney, string TroubleType, string AddNoteMan, string YingXiang, string ProcessDept, string[] parts)
        {
            DeviceInfo devinfo = null;
            bool bResult = false;
            TroubleCode = TroubleCode.Replace("'", "''");
            TroubleCom = TroubleCom.Replace("'", "''");
            ProcessWay = ProcessWay.Replace("'", "''");
            TroubleReason = TroubleReason.Replace("'", "''");
            HappenTime = HappenTime.Replace("'", "''");

            RepairTime = RepairTime.Replace("'", "''");
            NeedMoney = NeedMoney.Replace("'", "''");
            TroubleType = TroubleType.Replace("'", "''");
            AddNoteMan = AddNoteMan.Replace("'", "''");
            ProcessDept = ProcessDept.Replace("'", "''");
            try
            {
                devinfo = new DeviceInfo();

                for (int i = 0; i < parts.Length; i++)
                {
                    parts[i] = parts[i].Replace("'", "''");
                }
                bResult = devinfo.setTroubleInfo(tid, devid, TroubleCode, TroubleCom, ProcessWay, TroubleReason, HappenTime, RepairTime, NeedMoney, TroubleType, AddNoteMan, YingXiang, ProcessDept, parts);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (devinfo != null)
                {
                    devinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion

        #region 获得备件信息

        [Ajax.AjaxMethod]
        public DataTable getPartInfo(string sIds)
        {
            DeviceInfo deviceinfo = new DeviceInfo();
            DataTable dt = null;
            try
            {
                dt = deviceinfo.getResultDevPartInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            return dt;
        }

        #endregion
    }
}